package com.jsmile.cloud.authc.sys.controller;

import javax.validation.Valid;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jsmile.cloud.authc.api.req.sys.ReqSysUserQuery;
import com.jsmile.cloud.authc.api.req.sys.ReqSysUserUpdate;
import com.jsmile.cloud.authc.api.res.ResSysUser;
import com.jsmile.cloud.authc.sys.SysUserApplicationService;
import com.jsmile.mail.security.controller.AuthcController;
import com.jsmile.mail.security.handler.JsmileContext;
import com.jsmile.mall.api.JSmileResult;
import com.jsmile.mall.api.annotation.CurrentContext;
import com.jsmile.mall.api.constants.JSmileConstant;
import com.jsmile.mall.api.enums.LogTypeEnum;
import com.jsmile.mall.api.to.JsmilePage;
import com.jsmile.mall.log.annotation.JsmileLog;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author 龚亮
 * @version 1.0
 * @desc 控制器
 * @date 2018年08月25日 04:57:24
 */
@RestController
@Api(tags = "用户管理")
@Slf4j
@AllArgsConstructor
@RequestMapping("/sys/user")
public class SysUserController extends AuthcController {

    private final SysUserApplicationService sysUserApplicationService;

    @RequestMapping(value = "/findAll", method = RequestMethod.POST)
    @ApiOperation(value = "用户分页列表")
    public JSmileResult<JsmilePage<ResSysUser>> getPageList(@RequestBody @Valid ReqSysUserQuery reqSysUserQuery, @CurrentContext JsmileContext jsmileContext) {
        reqSysUserQuery.setCurTenantId(jsmileContext.getTenantId());
        IPage<ResSysUser> pages = sysUserApplicationService.getPageList(reqSysUserQuery);
        return JSmileResult.success(new JsmilePage(reqSysUserQuery.getPageIndex(), reqSysUserQuery.getPageSize(), pages.getTotal(), pages.getRecords()));
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiOperation(value = "新增文档")
    @JsmileLog(title = "用户中心->新增用户", type = LogTypeEnum.INSERT, systemId = JSmileConstant.SystemId.AUTHC)
    public JSmileResult saveUser(@RequestBody @Valid ReqSysUserUpdate reqSysUserUpdate, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.AUTHC);
        reqSysUserUpdate.setTenantId(jsmileContext.getTenantId());
        reqSysUserUpdate.setCreateBy(jsmileContext.getUserName());
        reqSysUserUpdate.setUpdateBy(jsmileContext.getUserName());
        reqSysUserUpdate.setPassword(new BCryptPasswordEncoder().encode(reqSysUserUpdate.getUserName()));
        if (!sysUserApplicationService.saveUser(reqSysUserUpdate)) {
            return JSmileResult.error("添加用户异常!");
        }
        return JSmileResult.success();
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "修改用户")
    @JsmileLog(title = "用户中心->修改用户", type = LogTypeEnum.UPDATE, systemId = JSmileConstant.SystemId.AUTHC)
    public JSmileResult updateUser(@RequestBody @Valid ReqSysUserUpdate reqSysUserUpdate, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.AUTHC);
        reqSysUserUpdate.setTenantId(jsmileContext.getTenantId());
        reqSysUserUpdate.setCreateBy(jsmileContext.getUserName());
        reqSysUserUpdate.setUpdateBy(jsmileContext.getUserName());
        if (!sysUserApplicationService.updateUser(reqSysUserUpdate)) {
            return JSmileResult.error("修改文档异常!");
        }
        return JSmileResult.success();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiOperation(value = "删除用户")
    @JsmileLog(title = "用户中心->删除用户", type = LogTypeEnum.DELETE, systemId = JSmileConstant.SystemId.AUTHC)
    public JSmileResult deleteUser(@RequestBody @Valid ReqSysUserUpdate reqSysUserUpdate, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.AUTHC);
        reqSysUserUpdate.setTenantId(jsmileContext.getTenantId());
        if (!sysUserApplicationService.deleteUser(reqSysUserUpdate)) {
            return JSmileResult.error("删除用户异常!");
        }
        return JSmileResult.success();
    }
}
